<template>
	<div class="wrapper">
		<!--header-->
		<header>
			<div class="back">
				<p @click="goBack">&lt;</p>
			</div>
			<p>用户地址</p>
		</header>
		<!--列表-->
		<ul class="addresslist">
			<li v-for="item in deliveryAddressArr">
				<div class="addressLeft" @click="setDeliveryAddress(item)">
					<h3>{{item.contactName}}{{sexJudge(item.contactSex)}} {{item.contactTel}}</h3>
					<p>{{item.address}}</p>
				</div>
				<div class="addressRight">
					<i class="fa fa-edit" @click="editUserAddress(item.daId)"></i>
					<i class="fa fa-remove" @click="removeUserAddress(item.daId)"></i>
				</div>
			</li>
		</ul>
		<!--新增-->
		<div class="addbtn" @click="toAddUserAddress">
			<i class="fa fa-plus-circle"></i>
			<p>新增收货地址</p>
		</div>
		
		<Footer></Footer>
	</div>
</template>

<script>
	import Footer from '../components/Footer.vue';
	
	
	export default{
		name:'UserAddress',
		data(){
			return{
				businessId:this.$route.query.businessId,
				user:{},
				deliveryAddressArr:[],
			}
		},
		created() {
			this.user = this.$getSessionStorage('user');
			this.listDeliveryAddressByUserId();
		},
		filters:{
			sexFilter(sex){
				return sex==1?'先生':'女士';
			}
		},
		components:{
			Footer
		},
		methods:{
			sexJudge(sex){
				return sex === 1 ? '先生':'女士';
			},
			    goBack() {
			      this.$router.go(-1);
			    },
			listDeliveryAddressByUserId() {
				//通过userId查询当前用户的所有地址信息
				this.$axios.post('DeliveryAddressController/listDeliveryAddressByUserId', this.$qs.stringify({
					userId: this.user.userId
				})).then(response => {
					this.deliveryAddressArr = response.data;
				}).catch(error => {
					console.error(error);
				});
			},
			setDeliveryAddress(deliveryAddress) {
				//把用户选择的默认送货地址存储到localStorage中
				this.$setLocalStorage(this.user.userId, deliveryAddress);
				this.$router.push({
					path: '/order',
					query: {
						businessId: this.businessId
					}
				});
			},
			//新增配送地址 
			toAddUserAddress() {
				this.$router.push({
					path: '/addUserAddress',
					query: {
						businessId: this.businessId
					}
				});
			},
			//编辑配送地址信息
			editUserAddress(daId) {
				this.$router.push({
					path: '/editUserAddress',
					query: {
						businessId: this.businessId,
						daId: daId
					}
				});
			},
			//删除配送地址信息
			removeUserAddress(daId) {
				if (!confirm('确认要删除此送货地址吗？')) {
					return;
				}
				//判断删除地址存在且状态正确
				this.$axios.post('DeliveryAddressController/removeDeliveryAddress', this.$qs.stringify({
					daId: daId,
				})).then(response => {
					if (response.data > 0) {
						let deliveryAddress = this.$getLocalStorage(this.user.userId);
						if (deliveryAddress != null && deliveryAddress.daId == daId) {
							this.$removeLocalStorage(this.user.userId);
						}
						this.listDeliveryAddressByUserId();
					} else {
						alert('删除地址失败！');
					}
				}).catch(error => {
					console.error(error);
				});
			}
		}
	}
</script>

<style scoped>
	/*wrapper*/
	.wrapper {
		width: 100%;
		height: 100%;
		background-color: #F5F5F5;
	}

	/*header*/
	/****************header*********************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper header .back {
		position: absolute;
		width: 2vw;
		height: 2vw;
		left: 1.5vw;
		top: 2.8vw;
		user-select: none;
		cursor: pointer;
	}

	/*addresslist*/
	.wrapper .addresslist {
		width: 100%;
		margin-top: 12vw;
		background-color: #FFFFFF;
	}

	.wrapper .addresslist li {
		width: 100%;
		box-sizing: border-box;
		border-bottom: solid 1px #DDDDDD;
		padding: 3vw;

		display: flex;
	}

	.wrapper .addresslist li .addressLeft {
		flex: 5;
		user-select: none;
		cursor: pointer;
	}

	.wrapper .addresslist li .addressLeft h3 {
		font-size: 4.6vw;
		font-weight: 300;
		color: #666666;
	}

	.wrapper .addresslist li .addressLeft p {
		font-size: 4vw;
		color: #666666;
	}

	.wrapper .addresslist li .addressRight {
		flex: 1;
		font-size: 5.6vw;
		color: #999999;
		cursor: pointer;

		display: flex;
		justify-content: center;
		align-items: center;
	}

	/*新增*/
	.wrapper .addbtn {
		width: 100%;
		height: 14vw;
		border-top: solid 1px #DDDDDD;
		border-bottom: solid 1px #DDDDDD;
		background-color: #FFFFFF;
		margin-top: 4vw;

		display: flex;
		justify-content: center;
		align-items: center;

		font-size: 4vw;
		color: #0097FF;
		user-select: none;
		cursor: pointer;
	}

	.wrapper .addbtn p {
		margin-left: 2vw;
	}
</style>
